package com.example.springbootdemo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class ParamController {

    @RequestMapping("/m1")
    public String method1(String name) {
        return "接收到参数name:" + name;
    }

    @RequestMapping("/m2")
    public String method2(Integer age){
        return "接收到参数 age:" + age;
    }

    @RequestMapping("/m3")
    public String method3(int age){
        return "接收到参数 age:" + age;
    }

    @RequestMapping("/m4")
    public String method4(String name, String password){
        return "接收到参数 name:" + name + "接收到参数password" + password;
    }

    @RequestMapping("/m5")
    public String method5(Person person){
        return "接收到参数 person:" + person;
    }

    @RequestMapping("/m6")
    public String method6(@RequestParam("age") Integer createage){
        return "接收到参数 createage:" + createage;
    }

    @RequestMapping("/m7")
    public String method7(@RequestParam(value = "age" ,required = false) Integer createage){
        return "接收到参数 createage:" + createage;
    }

    @RequestMapping("/m8")
    public String method8(String[] arr){
        return"数组" + Arrays.toString(arr) + "length:" + arr.length;
    }

    @RequestMapping("/m9")
    public String method9(@RequestParam List<String> list ){
        return "list:" + list + "size:" + list.size() ;
    }


    @RequestMapping("/m10")
    public String method10(@RequestBody Person person ){
        return "接收JSON数据" + person.toString() ;
    }


    @RequestMapping("/m11/article/{Id}/{type}")
    public String method11(@PathVariable("Id") Integer articleId, @PathVariable String type){
        return "文章ID:" + articleId + ", type:" + type;
    }

    @RequestMapping("/m12")
    public String method12(@RequestPart("file1") MultipartFile file) throws IOException {
        // 获取文件名称
        String originalFilename = file.getOriginalFilename();
        // 文件上传到指定路径
        File destFile = new File("D:\\temp\\"+ originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "存储文件成功，接收到文件：" + originalFilename ;
    }

    @RequestMapping("/m13")
    public String method13(@RequestParam("file2") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File destFile = new File("D:\\temp\\"+ originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "存储文件成功，接收到文件：" + originalFilename ;
    }

    // 方法一：通过 HttpServletRequest 获取
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        // 1. 从 HttpServletRequest 对象中获取客户端发送的所有 Cookie
        Cookie[] cookies = request.getCookies();
        // 2. 安全检查：判断 cookies 数组是否为空（即客户端是否发送了cookie）
        if (cookies!=null) {
            // 3. 遍历 cookies 数组
            for (Cookie cookie: cookies){
                // 4. 打印每一个 cookie 的名称和值
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("name") String name){
        return "获取cookie成功" + name;
    }


    @RequestMapping("/setSession")
    public String setSession(String name, HttpServletRequest request){
        // 1. 从 request 中获取 Session 对象
        HttpSession session = request.getSession();
        // 2. 向 session 中存储数据（属性）
        session.setAttribute("name", name);
        session.setAttribute("age", 1);
        return "获取session成功";
    }

    @RequestMapping("/setSession2")
    public String setSession2(String name, HttpSession session){
//        HttpSession session = request.getSession();
        session.setAttribute("name", name);
        session.setAttribute("age", 1);
        return "获取session成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        // 1. 获取已存在的 session，如果不存在则返回 null
        HttpSession session = request.getSession(false);
        // 2. 从 session 中读取属性并强制转换类型
        String name = (String)session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "从session中获取信息, name:" +name + ", age:" + age;
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        String name = (String)session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "从session中获取信息, name:" +name + ", age:" + age;
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name){
        return "从session中获取信息, name:" +name;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取信息, userAgent:" +userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "从header中获取信息, userAgent:" +userAgent;
    }


}